package control;

import Entity.orderBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class orderControl {
    public orderControl() {
    }
    //获取订单列表（type为1时获取已支付的订单，否则获取全部类型的订单）
    public static void get_orderlist(HttpServletRequest request, HttpServletResponse response,int type)
    {
        try{
            Connection con=null;
            Statement stmt1=null,stmt2=null,stmt3=null;
            ResultSet rs=null,rs2=null,rs3=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            //System.out.println("到达");
            con= DriverManager.getConnection(url,"root","root");
            //System.out.println("到达2");
            HttpSession session=request.getSession();
            session.removeAttribute("orderBean");
            stmt1=con.createStatement();
            stmt2=con.createStatement();
            stmt3=con.createStatement();
            String sql="select * from `order`";
            rs=stmt1.executeQuery(sql);
            ArrayList orderlist=null;
            if((ArrayList)session.getAttribute("orderlist")==null){
                orderlist=new ArrayList();
                while(rs.next()){
                    orderBean ff = new orderBean();
                    ff.setOrder_id(rs.getString("order_id"));
                    ff.setState(rs.getString("state"));
                    ff.setWay(rs.getString("way"));
                    ff.setTime(rs.getString("time"));
                    ff.setAddress(rs.getString("address"));
                    ff.setUse_id(rs.getString("use_id"));
                    ff.setGoods_id(rs.getString("goods_id"));
                    String sql2="select * from goods where goods_id = "+ rs.getString("goods_id");
                    rs2=stmt2.executeQuery(sql2);
                    while(rs2.next()) {
                        ff.setGoods_img(rs2.getString("goods_img"));
                        ff.setGoods_price(rs2.getString("goods_price"));
                        ff.setName(rs2.getString("name"));
                    }
                    String sql3="select * from shop_user where use_id = "+ rs.getString("use_id");
                    rs3=stmt3.executeQuery(sql3);
                    while(rs3.next()) {
                        ff.setUsername(rs3.getString("username"));
                    }
                    System.out.println(ff);
                    if (type == 1 ) {
                        if (ff.getState().equals("已支付"))
                            orderlist.add(ff);
                    }
                    else{
                        orderlist.add(ff);
                    }
                }
                session.setAttribute("orderlist", orderlist);
            }
            rs.close();
            rs2.close();
            rs3.close();
            stmt1.close();
            stmt2.close();
            stmt3.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return;
    }

}
